package cn.mado.org.controller;

import cn.mado.basic.note.SysLog;
import cn.mado.basic.util.AjaxResult;
import cn.mado.basic.util.PageList;
import cn.mado.org.domain.User;
import cn.mado.org.query.UserQuery;
import cn.mado.org.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 查询部门全部
     */
    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    /**
     * 添加、修改部门
     */
    @SysLog("添加修改了一个用户")
    @PostMapping("save")
    public AjaxResult addOrUpdate(@RequestBody User user){
        System.out.println("我来："+user);
        if (null == user.getUserId()){
            // 添加
            userService.add(user);
        }else {
            // 修改
            userService.update(user);
        }
        return AjaxResult.me().success();
    }
    /**
     * 删除 delete localhost:8080/user/1
     */
    @SysLog("删除了一个用户")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable("userId") Long userId){
        System.out.println(userId);
        userService.delete(userId);
        return AjaxResult.me().success();
    }


    /**
     * 根据id查询一个
     */
    @GetMapping("/{userId}")
    public User findById(@PathVariable("userId") Long userId){
        System.out.println(userId);
        return userService.findById(userId);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<User> queryByPage(@RequestBody UserQuery query){
        return userService.queryByPage(query);
    }

}
